﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using KM.Models;

namespace KM.HtmlHelpers
{
    public static class RecordPager
    {
        public static MvcHtmlString RecordPage(this HtmlHelper html, PagingInfoModel pageInfo, Func<int, string> _url)
        {

            StringBuilder result = new StringBuilder();

            for (int i = 1; i <= pageInfo.TotalPages; i++)
            {
                TagBuilder builder = new TagBuilder("a");
                builder.MergeAttribute("href", _url(i));
                builder.InnerHtml = i.ToString();
                if (i == pageInfo.CurrentPage)
                    builder.AddCssClass("current");
                result.Append(builder.ToString());
            }

            return MvcHtmlString.Create(result.ToString());
        }
        public static MvcHtmlString PhanTrang(this HtmlHelper html, PagingInfoModel pageInfo, Func<int, string> _url)
        {
            StringBuilder result = new StringBuilder();
            TagBuilder builder;
            result.Append("<div class=\"dataTables_paginate paging_bootstrap\">");
            result.Append("<ul class=\"pagination\">");

            int pageSize = pageInfo.recordPerPage;
            int recordCount = 0;
            int pageView = 9;
            int pageMidle = pageView / 2 + 1;
            double dblPageCount = (double)((decimal)recordCount / decimal.Parse(pageSize.ToString()));
            int pageCount = (int)Math.Ceiling(dblPageCount);

            builder = new TagBuilder("a");

            if (pageInfo.CurrentPage != 1)
            {
                builder.MergeAttribute("href", _url(1));
                builder.InnerHtml = "First";
                result.Append("<li >" + builder.ToString() + "</li>");
            }

            if (pageInfo.TotalPages > pageView)
            {
                //Nếu trang hiện tại <X/2 thì hiển thị từ 1 đến X
                if (pageInfo.CurrentPage < pageMidle)
                {
                    for (int i = 1; i <= pageView; i++)
                    {
                        builder = new TagBuilder("a");
                        builder.MergeAttribute("href", _url(i));
                        builder.InnerHtml = i.ToString();
                        if (i == pageInfo.CurrentPage)
                        {
                            result.Append("<li class=\"active\" >" + builder.ToString() + "</li>");
                        }
                        else
                        {
                            result.Append("<li >" + builder.ToString() + "</li>");
                        }
                    }
                }
                else
                {
                    //Nếu trang hiện tại >X/2 va nho hon tong so page-midel thì hiển thị từ 
                    if (pageInfo.CurrentPage >= pageMidle && pageInfo.CurrentPage < pageInfo.TotalPages - pageMidle)
                    {
                        for (int i = pageInfo.CurrentPage - pageMidle + 1; i <= pageInfo.CurrentPage + pageMidle - 1; i++)
                        {
                            builder = new TagBuilder("a");
                            builder.MergeAttribute("href", _url(i));
                            builder.InnerHtml = i.ToString();
                            if (i == pageInfo.CurrentPage)
                            {
                                result.Append("<li class=\"active\" >" + builder.ToString() + "</li>");
                            }
                            else
                            {
                                result.Append("<li >" + builder.ToString() + "</li>");
                            }
                        }
                    }
                    else
                    {
                        //Nếu trang hiện tại >X/2 thì hiển thị từ 
                        if (pageInfo.CurrentPage >= pageInfo.TotalPages - pageMidle)
                        {
                            for (int i = pageInfo.TotalPages - pageView; i <= pageInfo.TotalPages; i++)
                            {
                                builder = new TagBuilder("a");
                                builder.MergeAttribute("href", _url(i));
                                builder.InnerHtml = i.ToString();
                                if (i == pageInfo.CurrentPage)
                                {
                                    result.Append("<li class=\"active\" >" + builder.ToString() + "</li>");
                                }
                                else
                                {
                                    result.Append("<li >" + builder.ToString() + "</li>");
                                }
                            }
                        }
                    }
                }
            }

            else
            {
                for (int i = 1; i <= pageInfo.TotalPages; i++)
                {
                    builder = new TagBuilder("a");
                    builder.MergeAttribute("href", _url(i));
                    builder.InnerHtml = i.ToString();
                    if (i == pageInfo.CurrentPage)
                    {
                        result.Append("<li class=\"active\" >" + builder.ToString() + "</li>");
                    }
                    else
                    {
                        result.Append("<li >" + builder.ToString() + "</li>");
                    }
                }
            }


            if (pageInfo.CurrentPage <= pageInfo.TotalPages - pageView + pageMidle)
            {
                builder = new TagBuilder("a");
                builder.MergeAttribute("href", _url(pageInfo.TotalPages));
                builder.InnerHtml = "Last";

                result.Append("<li >" + builder.ToString() + "</li>");
            }

            result.Append("</ul>");
            result.Append("</div>");

            return MvcHtmlString.Create(result.ToString());
        }

    }
}